package com.fyzk.activity.user.accountdetail;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fyzk.bean.WithdrawInfo;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tyjc.lottery.R.id.withdraw_account;

/**
 * 账户明细
 */
public class WithdrawListAdapter extends BaseAdapter {

    protected Context context;
    protected List<WithdrawInfo> list = new ArrayList<>();

    public WithdrawListAdapter(Context context, List<WithdrawInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {

        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {

        WithdrawInfo infor = list.get(arg0);

        arg1 = LayoutInflater.from(context).inflate(R.layout.item_withdrawrecord, null);
        ViewHolder holder = new ViewHolder();
        holder.withdraw_amount = (TextView) arg1.findViewById(R.id.withdraw_amount);
        holder.time = (TextView) arg1.findViewById(R.id.zhhmx_list_time);
        holder.time1 = (TextView) arg1.findViewById(R.id.zhhmx_list_time1);
        holder.withdraw_account = (TextView) arg1.findViewById(withdraw_account);
        holder.withdraw_state = (TextView) arg1.findViewById(R.id.withdraw_state);

        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("MM-dd");
        try {
            long createTime = Long.parseLong(infor.getCreateTime());

            holder.time.setText(dateFormat.format(new Date(createTime)));
            holder.time1.setText(dateFormat1.format(new Date(createTime)));
        } catch (Exception e) {

        }

        int withdrawAmount = Integer.parseInt(infor.getAmount()) / 100;

        holder.withdraw_amount.setText(withdrawAmount+"");
        arg1.setTag(R.string.app_name,infor);

        arg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,WithDrawRecordDetailActivity.class);
                WithdrawInfo infor = (WithdrawInfo) v.getTag(R.string.app_name);
                intent.putExtra(WithDrawRecordDetailActivity.ID,infor.getId());
                if(infor.getDrawType() == 1){//银行卡提现
                    intent.putExtra(WithDrawRecordDetailActivity.TYPE,"银行卡提现");
                    intent.putExtra(WithDrawRecordDetailActivity.ACCOUNT,infor.getBankName()+infor.getBankId());
                }else if(infor.getDrawType() == 2){//支付宝提现
                    intent.putExtra(WithDrawRecordDetailActivity.TYPE,"支付宝提现");
                    intent.putExtra(WithDrawRecordDetailActivity.ACCOUNT,infor.getBankId());
                }
                intent.putExtra(WithDrawRecordDetailActivity.STARTTIME,infor.getCreateTime());
                intent.putExtra(WithDrawRecordDetailActivity.FINISHTIME,infor.getFinishTime());
                intent.putExtra(WithDrawRecordDetailActivity.AMOUNT,infor.getAmount());
                intent.putExtra(WithDrawRecordDetailActivity.DESCRIPTION,infor.getDescription());
                intent.putExtra(WithDrawRecordDetailActivity.STATUS,infor.getStatusMemo());
                intent.putExtra(WithDrawRecordDetailActivity.STATUSVALUE,infor.getStatus());
                context.startActivity(intent);
            }
        });
        String statusMemo = infor.getStatusMemo();
        if (statusMemo.contains("处理中")){
            holder.withdraw_state.setTextColor(Color.parseColor("#5C96F9"));
        }else if (statusMemo.contains("失败")){
            holder.withdraw_state.setTextColor(Color.parseColor("#FD4A4A"));
        }else {
            holder.withdraw_state.setTextColor(Color.parseColor("#1a1a1a"));
        }
        holder.withdraw_state.setText(statusMemo);
        String tag = "";
        arg1.setTag(holder);
        return arg1;
    }

    class ViewHolder {
        public TextView time1;
        TextView time;
        TextView withdraw_amount;
        TextView withdraw_account;
        TextView withdraw_state;
    }

    public void setList(List<WithdrawInfo> list) {

        this.list = list;
        this.notifyDataSetChanged();
    }

    public List<WithdrawInfo> getList() {
        return this.list;
    }

}
